package com.example.english.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.english.App;
import com.example.english.R;
import com.example.english.Utils;
import com.example.english.db.UserBean;

import java.util.ArrayList;
import java.util.List;

public class RegisterActivity extends AppCompatActivity {

    private EditText et_account,et_pwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Utils.setBarColor(this,true);
        et_account = findViewById(R.id.et_account);
        et_pwd = findViewById(R.id.et_pwd);
    }

    public void onRegisterUser(View view){
        if(TextUtils.isEmpty(et_account.getText().toString()) || TextUtils.isEmpty(et_pwd.getText().toString())){
            Toast.makeText(this ,"请输入账号密码",Toast.LENGTH_LONG).show();
            return;
        }

        List<UserBean> beanList =App.dbsqLite.getList();
        boolean isAccount = false;
        for (int i = 0; i < beanList.size(); i++){
            if(et_account.getText().toString().equals(beanList.get(i).getAccount())){
                isAccount = true;
                break;
            }
        }
        if(isAccount){
            Toast.makeText(this ,"当前账号已注册",Toast.LENGTH_LONG).show();
            return;
        }

        UserBean bean = new UserBean();
        bean.setAccount(et_account.getText().toString());
        bean.setPassword(et_pwd.getText().toString());
        App.dbsqLite.add(bean);
        Toast.makeText(RegisterActivity.this ,"注册成功",Toast.LENGTH_LONG).show();
        finish();
    }

    public void onFinish(View view){
        finish();
    }

}
